package com.ysxsoft.wdg.ui.activity.my;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.classic.common.MultipleStatusView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.ysxsoft.wdg.R;
import com.ysxsoft.wdg.adapter.QuanAdapter;
import com.ysxsoft.wdg.base.BaseActivity;
import com.ysxsoft.wdg.bean.home.MessageBean;
import com.ysxsoft.wdg.bean.response.CouponListResponse;
import com.ysxsoft.wdg.bean.response.MyRedPackBean;
import com.ysxsoft.wdg.net.CallbackCode;
import com.ysxsoft.wdg.net.UrlHost;
import com.ysxsoft.wdg.util.JsonUtils;
import com.ysxsoft.wdg.util.sp.SharePrefUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

public class MyQuanActivity extends BaseActivity implements OnRefreshListener, OnLoadmoreListener {

    public static void start(Context context) {
        Intent intent = new Intent(context, MyQuanActivity.class);
        context.startActivity(intent);
    }


    @BindView(R.id.topView)
    View topView;
    @BindView(R.id.title_finish)
    ImageView titleFinish;
    @BindView(R.id.title_content)
    TextView titleContent;
    @BindView(R.id.tv0)
    TextView tv0;
    @BindView(R.id.tv1)
    TextView tv1;
    @BindView(R.id.tv2)
    TextView tv2;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.multStatusView)
    MultipleStatusView multStatusView;

    private QuanAdapter adapter;
    private int type = 1;
    private int page = 1;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_my_quan;
    }

    @Override
    protected void initData() {
        initStatusBar(topView, false);
        titleContent.setText("我的优惠券");
        tv0.setSelected(true);

        recyclerview.setLayoutManager(new LinearLayoutManager(this));
        adapter = new QuanAdapter(R.layout.item_quan);
        recyclerview.setAdapter(adapter);

//        List<String> list = new ArrayList<>();
//        for (int i = 0; i < 4; i++) {
//            list.add("");
//        }
//        adapter.setNewData(list);
        type = 1;
//        adapter.setType(type);

        getCouponList();
    }

    @Override
    protected void setListener() {
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
//                if (type == 0) {
//                }
                CouponListResponse.DataBean data = (CouponListResponse.DataBean) adapter.getItem(position);
            }
        });
    }

    @OnClick({R.id.title_finish, R.id.tv0, R.id.tv1, R.id.tv2})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_finish:
                finish();
                break;
            case R.id.tv0:
                tv0.setSelected(true);
                tv1.setSelected(false);
                tv2.setSelected(false);
                type = 1;
                getCouponList();
                break;
            case R.id.tv1:
                tv0.setSelected(false);
                tv1.setSelected(true);
                tv2.setSelected(false);
                type = 2;
                getCouponList();
                break;
            case R.id.tv2:
                tv0.setSelected(false);
                tv1.setSelected(false);
                tv2.setSelected(true);
                type = 3;
                getCouponList();
                break;
        }
    }

    /**
     * 获取优惠券
     */
    private void getCouponList() {
        multStatusView.showLoading();
        OkHttpUtils.post()
                .url(UrlHost.COUPON_LIST)
                .addParams("uid",  SharePrefUtils.getUserId())
                .addParams("page", page + "")
                .addParams("type", type + "")//优惠券类型
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                if (!call.isCanceled()) {
                    multStatusView.showError();
                    reset();
                }
            }

            @Override
            public void onResponse(String response, int id) {
                reset();
                if (response != null) {
                    multStatusView.showContent();
                    CouponListResponse messageBean = JsonUtils.parseByGson(response, CouponListResponse.class);
                    if (messageBean == null) {
                        return;
                    }
                    if (CallbackCode.SUCCESS.equals(messageBean.getCode())) {
                        List<CouponListResponse.DataBean> list = messageBean.getData();
                        if (page == 1) {
                            adapter.setNewData(list);
                        } else {
                            adapter.addData(list);
                        }
                    } else if (CallbackCode.EMPTY.equals(messageBean.getCode())) {
                        if(page==1){
                            multStatusView.showEmpty();
                        }
                    }
                } else {
                    multStatusView.showError();
                }
            }
        });
    }

    private void reset() {
        if (refreshLayout.isRefreshing()) {
            refreshLayout.finishRefresh(true);
        }
        refreshLayout.finishLoadmore();
    }

    @Override
    public void onLoadmore(RefreshLayout refreshlayout) {
        page++;
        getCouponList();
    }

    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        page = 1;
        getCouponList();
    }
}
